package listen;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import service.ImageService;
import service.impl.ImageServiceImpl;

import javax.swing.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Predicate;

/**
 * @Author: gaoxu11
 * @Date: 2022/5/13 12:45 AM
 * @Description:GImG.KeyboardListen: 键盘快捷键监听
 */
@Slf4j
public class KeyboardListener implements NativeKeyListener {

    protected final static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue();

    private String osName = System.getProperty("os.name");

    private ImageService imageService = new ImageServiceImpl();

    @Override
    public void nativeKeyTyped(NativeKeyEvent nativeEvent) {
        NativeKeyListener.super.nativeKeyTyped(nativeEvent);
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent nativeEvent) {
        hotSpotKeyboard(nativeEvent);
        NativeKeyListener.super.nativeKeyPressed(nativeEvent);
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent nativeEvent) {
        NativeKeyListener.super.nativeKeyReleased(nativeEvent);
    }

    /**
     * 热键
     *
     * @param nativeEvent 按键事件
     */
    public void hotSpotKeyboard(NativeKeyEvent nativeEvent) {
        //当有键盘输入的时候，放入队列
        try {
            queue.put(nativeEvent.getKeyCode());
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }


        // Ctrl + alt + p
        int[] hotKeyArray = {NativeKeyEvent.VC_CONTROL, StringUtils.equals(osName, "Mac OS X") ? NativeKeyEvent.VC_META : NativeKeyEvent.VC_ALT, NativeKeyEvent.VC_P};
        //如果队列中的数据大于等于3，那就进行判断是不是包含连续且等于我们指定的键的顺序
        //如果存在就进行执行处罚
        if (queue.size() >= 3 && judgeCombinationKey(hotKeyArray)) {
            try {
                imageService.upload();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(null, "[已将链接粘贴到剪切板]");
                    }
                }).start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            queue.clear();
        }
        if (queue.size() == 4) {
            queue.poll();
        }

    }

    protected Boolean judgeCombinationKey(int[] hotKeyArray) {
        Object[] queueKey = queue.toArray();

        Predicate<int[]> keyArrayPredicateOne = hotKeies -> (int) queueKey[0] == hotKeies[0]
                && (int) queueKey[1] == hotKeies[1]
                && (int) queueKey[2] == hotKeies[2];

        Predicate<int[]> keyArrayPredicateTwo = hotKeies -> (int) queueKey[1] == hotKeies[0]
                && (int) queueKey[2] == hotKeies[1]
                && (int) queueKey[3] == hotKeies[2];

        return queue.size() == 3 ? keyArrayPredicateOne.test(hotKeyArray) :
                keyArrayPredicateOne.or(keyArrayPredicateTwo).test(hotKeyArray);

    }
}
